package fs.component.popup {
	import flash.desktop.NativeApplication;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Point;
	/**
	 * ...
	 * @author F.S.
	 */
	public class PopupManagement extends Sprite{
		private var _dark_layer:Sprite;
		private var _dialog_layer:Sprite;
		private static var _vctPopupManagement:Vector.<PopupManagement> = new Vector.<PopupManagement>();
		private var _disObject:DisplayObject;
		
		public function PopupManagement(useFactoryMethold:Object) {		
			_vctPopupManagement.push(this);
			
			_dark_layer = new Sprite();
			addChild(_dark_layer);
			_dark_layer.graphics.beginFill(0, 0.3);
			_dark_layer.graphics.drawRect(0, 0, 100, 100);
			_dark_layer.graphics.endFill();
			
			_dialog_layer = new Sprite();
			//_dialog_layer.graphics.beginFill(0x808080);
			//_dialog_layer.graphics.drawRect(0, 0, 100, 100);
			//_dialog_layer.graphics.endFill();
			addChild(_dialog_layer);
		}
		
		public static function popup(disObjClass:Class, parent:DisplayObjectContainer):DisplayObject {
			var instance:PopupManagement = new PopupManagement(null);
			instance.pop(disObjClass, parent);
			return instance._disObject;
		}
		private function pop(disObjClass:Class, parent:DisplayObjectContainer):DisplayObject {
			_disObject = new disObjClass() as DisplayObject;			
			
			_dialog_layer.addChild(_disObject);
			
			parent.addChild(this);
			//_type = type;
			
			if (stage == null) return _disObject;
			
			var local_point:Point = parent.globalToLocal(new Point());
			
			x = local_point.x;
			y = local_point.y;
			
			_dark_layer.width = stage.stageWidth;
			_dark_layer.height = stage.stageHeight;
			
			_disObject.x = stage.stageWidth / 2;
			_disObject.y = stage.stageHeight / 2;
			
			return _disObject;
		}
		
		public static function close(disObj:DisplayObject):void {
			for each(var ppm:PopupManagement in _vctPopupManagement) {
				if (ppm._disObject == disObj && ppm.parent) {
					ppm.parent.removeChild(ppm);
					_vctPopupManagement = _vctPopupManagement.filter(function (... args):Boolean{return args[0]!=ppm});
				}
			}
		}
	}

}